/**
  ******************************************************************************
  * File Name          : mqtt_app.c
  * Description        : This file provides code for the configuration
  *                      of the edp app instances.
  ******************************************************************************
  * @attention
  ******************************************************************************
  */
#include <stdio.h>
#include <string.h>
#include "tskcfg.h"
#include "network.h"
#include "cJSON.h"
#include "route.h"

void MQTT_RecvCallBack(struct MQTTContext * pContext, struct MQTTPacketInfo * info, struct MQTTDeserializedInfo * pack)
{
  Network_AcksType rack;
  char *ptopic;
  char *next;
  char *nnext;
  
  if((info == NULL) || (pack == NULL))
  {
    return;
  }
  
  switch(info->type)
  {
    case MQTT_PACKET_TYPE_PUBLISH:
      if((pack->pPublishInfo != NULL) && (pack->pPublishInfo->payloadLength < NETWORK_SUB_PAYLOAD_BUFFER_MAXLEN) && (pack->pPublishInfo->topicNameLength < NETWORK_SUB_TOPIC_BUFFER_MAXLEN))
      {
        memcpy(Network_Sub_topic, pack->pPublishInfo->pTopicName, pack->pPublishInfo->topicNameLength);
        Network_Sub_topic[pack->pPublishInfo->topicNameLength] = 0;
        
        memcpy(Network_Sub_payload, pack->pPublishInfo->pPayload, pack->pPublishInfo->payloadLength);
        Network_Sub_payload[pack->pPublishInfo->payloadLength] = 0;
        
        SysDbg_net_rx_tp("nrxt:%s\r\n", Network_Sub_topic);
        SysDbg_net_rx_pl("nrxp:%s\r\n", Network_Sub_payload);
        
        ptopic = Network_Sub_topic + 30;
        
        next = strstr(ptopic, "/service/");
        if(next != NULL)
        {
					if(strstr(next, "/lx_") != NULL)
					{
						if(strstr(next, "/lx_kg/") != NULL)
						{
							Route_lx_down_act(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						
						if(strstr(next, "/lx_sz/") != NULL)
						{
              Route_lx_down_para(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						
						if(strstr(next, "/lx_tb/") != NULL)
						{
              Route_lx_down_tb(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						return;
					}
					
          if(strstr(next, "/cf_") != NULL)
					{
						if(strstr(next, "/cf_kg/") != NULL)
						{
							Route_cf_down_act(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						
						if(strstr(next, "/cf_sz/") != NULL)
						{
              Route_cf_down_para(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						
						if(strstr(next, "/cf_tb/") != NULL)
						{
              Route_cf_down_tb(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						return;
					}
          
          if(strstr(next, "/xc_") != NULL)
					{
            if(strstr(next, "/xc_tb/") != NULL)
						{
							Route_xc_down_tb(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						
						if(strstr(next, "/xc_fw/") != NULL)
						{
              Route_xc_down_fw(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						
						if(strstr(next, "/xc_sj/") != NULL)
						{
              Route_xc_down_sj(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
						return;
          }

          if(strstr(next, "/sbtb/") != NULL)    /* 网关 设备同步 */
          {
            Route_gw_down_sbtb();
						return;
          }
          
          if(strstr(next, "/rl/") != NULL)    /* 日龄 */
          {
            Route_gw_down_rl(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/hkwatb/") != NULL)  /* 环控 外显数据同步 */
          {
            return;
          }
          
          if(strstr(next, "/ecsk/") != NULL)    /* 环控 时控模式参数 */
          {
            Route_ec_down_sk(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecdl/") != NULL)    /* 环控 独立模式1参数 */
          {
            Route_ec_down_dl1(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecdl1/") != NULL)   /* 环控 独立模式2参数 */
          {
            Route_ec_down_dl2(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecjt/") != NULL)    /* 环控 阶梯模式参数 */
          {
            Route_ec_down_jt(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecgdbj/") != NULL)  /* 环控 本地高低温独立报警温度 */
          {
            Route_ec_down_gdbj(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecjtxc/") != NULL)  /* 环控 小窗同步风机开合度 */
          {
            Route_ec_down_xckhd(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/tden/") != NULL)    /* 环控 通道参数 */
          {
            Route_ec_down_td(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/mode/") != NULL)    /* 环控 模式参数 */
          {
            Route_ec_down_md(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecall/") != NULL)   /* 环控 同步所有参数 */
          {
            Route_ec_down_allpara(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/tdsyn/") != NULL)   /* 环控 同步通道参数 */
          {
            Route_ec_down_tdpara(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecjttb/") != NULL)  /* 环控 同步阶梯参数 */
          {
            Route_ec_down_jtpara(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecdltp/") != NULL)  /* 环控 同步独立参数 */
          {
            Route_ec_down_dlpara(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/ecsktb/") != NULL)  /* 环控 同步时控参数 */
          {
            Route_ec_down_skpara(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/mufn/") != NULL)    /* 环控 手动风机指令 */
          {
            Route_ec_down_mufn(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/musl/") != NULL)    /* 环控 手动氺帘指令 */
          {
            Route_ec_down_musl(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/munq/") != NULL)    /* 环控 手动暖气指令 */
          {
            Route_ec_down_munq(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/muvf/") != NULL)    /* 环控 手动变频器指令 */
          {
            Route_ec_down_muvfd(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/sendTime/") != NULL)    /* 环控 手动变频器指令 */
          {
            Route_gw_down_time(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
                  
          if(strstr(next, "/dk/") != NULL)          /* 灯控, 普通灯 */
          {
            Route_dk_down_pt(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/dktg/") != NULL)        /* 灯控, 调光灯 */
          {
            Route_dk_down_tg(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
          
          if(strstr(next, "/cmd/") != NULL)         /* 网络指令 */
          {
            Route_gw_cmd(Network_Sub_payload, pack->pPublishInfo->payloadLength);
            return;
          }
        }
        
        next = strstr(ptopic, "/sub/");
        if(next != NULL)
        {
          nnext = strstr(next, "/topo/");
          if(nnext != NULL)
          {
            if(strstr(nnext, "/get/reply") != NULL)
            {
              Route_gw_down_gettopo(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
            }
            
            if(strstr(nnext, "/add/reply") != NULL)
            {
              Route_gw_down_addtopo(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
            }
						
						if(strstr(nnext, "/delete/reply") != NULL)
						{
							Route_gw_down_deltopo(Network_Sub_payload, pack->pPublishInfo->payloadLength);
							return;
						}
          }
        }
      }
      return;
    
    case MQTT_PACKET_TYPE_PUBACK:
    case MQTT_PACKET_TYPE_SUBACK:
    case MQTT_PACKET_TYPE_UNSUBACK:
      if(Network_Acks_qhd != NULL)
      {
        rack.type = info->type;
        rack.packetId = pack->packetIdentifier;
        rack.result = pack->deserializationResult;
        xQueueSend(Network_Acks_qhd, &rack, 0);
      }
      return;
    
    default:
      return;
  }
}
